﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using LiveSequence.Common.Tree;
using LiveSequence.Suite.Documents;
using LiveSequence.Suite.Domain;
using LiveSequence.Suite.Interfaces;
using LiveSequence.Suite.Interfaces;

namespace LiveSequence.Suite.Views
{
  internal partial class ProjectExplorer : BaseView<ProjectDoc>
  {
    private const int ASSEMBLY_INDEX = 1;
    private const int NAMESPACE_INDEX = 2;
    private const int TYPE_INDEX = 3;
    private const int METHOD_INDEX = 4;

    private DocManager _docManager;


    public ProjectExplorer()
    {
      InitializeComponent();

      

      _docManager = DocManager.Instance;
      _docManager.MasterDocumentChanged += new EventHandler(_docManager_MasterDocumentChanged);

      _tsbtnRemove.Enabled = false;
      _tsbtnRemove.Click += new EventHandler(_tsbtnRemove_Click);
      OnChangedProjectDoc();
    }

    void _tsbtnRemove_Click(object sender, EventArgs e)
    {
      TreeNode selectedNode = _treeView.SelectedNode;
      if (selectedNode != null)
      {
        LSMethodCallDiagram selectedDiagram = selectedNode.Tag as LSMethodCallDiagram;
        SelectedDocument.RemoveDiagram(selectedDiagram);
      }

    }

    void _docManager_MasterDocumentChanged(object sender, EventArgs e)
    {
      OnChangedProjectDoc();

    }


    private void OnChangedProjectDoc()
    {
      ProjectDoc projectDoc = _docManager.MasterDocument;
      if (projectDoc != null)
      {
        SelectedDocument = projectDoc;
      }
    }

    public override void  UpdateView(object obj)
    {
      _treeView.Nodes.Clear();

      LSProject project = SelectedDocument.Project;
      populateMethodDiagramTreeView(project);
      _treeView.ExpandAll();
    }

    private void populateMethodDiagramTreeView(LSProject project)
    {
      TreeNode _rootNode = new TreeNode("Project");
      _treeView.Nodes.Add(_rootNode);

      foreach (LSMethodCallDiagram diagram in project.Diagrams)
      {
        TreeNode diagramNode = new TreeNode(diagram.Title, METHOD_INDEX, METHOD_INDEX);
        _rootNode.Nodes.Add(diagramNode);

        diagramNode.Tag = diagram;
      }

    }

    private TreeNode insertNode(TreeNode node, string p, int iImageIndex)
    {
      TreeNode result = null;

      foreach (TreeNode child in node.Nodes)
      {
        if (child.Text == p)
        {
          result = child;
          break;
        }
      }
      
      if (result == null)
      {
        result = new TreeNode(p);
        if (iImageIndex != -1)
        {
          result.ImageIndex = iImageIndex;
          result.SelectedImageIndex = iImageIndex;
        }
        node.Nodes.Add(result);
      }
      return result;
    }

    private void _treeView_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e)
    {
      TreeNode selectedNode = e.Node;
      LSMethodCallDiagram selectedDiagram = selectedNode.Tag as LSMethodCallDiagram;
      if (selectedDiagram != null)
      {
        _docManager.ActiveDocument = new SeqDiagramDoc(selectedDiagram);
      }
    }

    private void _treeView_AfterSelect(object sender, TreeViewEventArgs e)
    {
      TreeNode selectedNode = e.Node;
      LSMethodCallDiagram selectedDiagram = selectedNode.Tag as LSMethodCallDiagram;
      if (selectedDiagram != null)
      {
        PropertyViewManager.Instance.SetObject(SelectedDocument, selectedDiagram);
      }

      _tsbtnRemove.Enabled = (selectedDiagram != null);
 
    }

    protected override void OnSelectedDocumentChanged()
    {
      base.OnSelectedDocumentChanged();

      _treeView.Nodes.Clear();

      LSProject project = SelectedDocument.Project;
      populateMethodDiagramTreeView(project);

    }
    
  }
}
